<template>
	<view>
		<view class="userInfo">
			<image :src="detaItem.avatar" mode="aspectFill"></image>
			<view class="font-32 font-weight">{{remark||detaItem.nick||'微信用户'}}</view>
		</view>

		<view class="p-30">
			<view v-for="(item,index) in data" :key="index" class="flex_b m-b-50" @click="routerFn(item)">
				<view class="flex">
					<image class="w-40 h-40 m-r-10" :src="item.img" mode=""></image>
					<view class="">{{item.title}}</view>
				</view>
				<view class="iconfont icon-xiangyoujiantou" style="font-size: 24rpx;"></view>
			</view>

		</view>
		<uv-toast ref="toast"></uv-toast>
	</view>
</template>

<script>
	import TencentCloudChat from '@tencentcloud/chat';
	export default {
		data() {
			return {
				detaItem: {},
				remark: '',
				userId: '',
				userInfo: {},
				UserSig: '',
				data: [{
						title: '设置备注名',
						img: require('../../static/icon/u1.png'),
						router: '/otherpages/userDetaName/userDetaName'
					},
					{
						title: '清空聊天记录',
						img: require('../../static/icon/u2.png')
					},
					{
						title: '解除配对',
						img: require('../../static/icon/u3.png')
					},
					{
						title: '举报',
						img: require('../../static/icon/u4.png'),
						router: '/otherpages/report/report'
					}
				]
			};
		},
		onLoad(res) {
			if (res.remark) {
				this.remark = res.remark
			}
			// 拉取用户信息
			let that = this
			let options = {
				SDKAppID: this.$config.sdkappid // 接入时需要将0替换为您的即时通信应用的 SDKAppID
			};
			let chat = TencentCloudChat.create(options); // SDK 实例通常用 chat 表示
			let promise = chat.getUserProfile({
				userIDList: [res.id] // 请注意：即使只拉取一个用户的资料，也需要用数组类型，例如：userIDList: ['user1']
			});
			promise.then(function(imResponse) {
				that.detaItem = imResponse.data[0]
				console.log('imResponse:', imResponse,
					'++++++---------====================='); // 存储用户资料的数组 - [Profile]
			}).catch(function(imError) {
				console.warn('getUserProfile error:', imError,
					'++++++---------====================='); // 获取其他用户资料失败的相关信息
			});
			this.getUserInfo()
			this.getUserDetali(res.id)
			this.userId = res.id
			console.log(res.id,"111111111111111111111111111111111111111111111111111111111111111111111")
			// this.setUserName()
		},
		onShow() {
			let that = this
			uni.$on('remark', data => {
				console.log('监听到事件来自返回的参数：', data);
				that.remark = data
			})
		},
		methods: {
			getUserSig() {
				this.$api.sendRequest({
					url: '/api/Tencent/genUserSig',
					success: res => {
						this.UserSig = res.data.UserSig
					}
				})
			},
			// 获取用户信息
			getUserInfo() {
				this.$api.sendRequest({
					url: '/api/user/userInfo',
					success: res => {
						this.userInfo = res.data
						this.getUserSig()
					}
				})
			},

			getUserDetali(id) {
				this.$api.sendRequest({
					url: '/api/Friends/userdetails',
					data: {
						user_id: id
					}
				})
			},
			routerFn(item) {
				let that = this
				let options = {
					SDKAppID: this.$config.sdkappid // 接入时需要将0替换为您的即时通信应用的 SDKAppID
				};
				let chat = TencentCloudChat.create(options); // SDK 实例通常用 chat 表示
				if (item.title == '清空聊天记录') {
					uni.showActionSheet({
						itemList: ['清空聊天记录'],
						success: res => {
							// 清空单聊的本地及云端的消息
							let promise = chat.clearHistoryMessage('C2C' + this.userId);
							promise.then(imResponse => {
								// 清空消息成功
								if (imResponse.code == 0) {
									that.$refs.toast.show({
										type: 'default',
										message: '删除成功',
										duration: 1500
									})
								}
							}).catch(function(imError) {
								console.warn('clearHistoryMessage error:', imError); // 清空消息失败的相关信息
							});
						},
						fail: res => {
							console.log(res.errMsg);
						}
					});
				} else if (item.title == '解除配对') {
					uni.showModal({
						title: '提示',
						content: '是否确认解除配对',
						success: res => {
							if (res.confirm) {
								console.log('用户点击确定');
								this.removeMatch()

							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					this.$util.redirectTo(item.router, {
						id: this.userId,
						remark: this.remark || this.detaItem.nick
					})
				}
			},
			// 解除配对
			removeMatch() {
				let that = this
				let options = {
					SDKAppID: this.$config.sdkappid // 接入时需要将0替换为您的即时通信应用的 SDKAppID
				};
				let chat = TencentCloudChat.create(options); // SDK 实例通常用 chat 表示
				this.$api.sendRequest({
					url: '/api/Friends/love',
					data: {
						type: '删除',
						user_id: this.userId
					},
					success: res => {
						// IM删除好友
						if (res.code == 1) {
							let promise = chat.deleteFriend({
							  userIDList: [this.userId],
							  type: TencentCloudChat.TYPES.SNS_DELETE_TYPE_BOTH
							});
							promise.then(function(imResponse) {
								console.log(imResponse,'---------------=================');
							  const { successUserIDList, failureUserIDList } = imResponse.data;
							  // 删除成功的 userIDList
							  successUserIDList.forEach((item) => {
							    const { userID } = item;
							  });
							  // 删除失败的 userIDList
							  failureUserIDList.forEach((item) => {
							    const { userID, code, message } = item;
							  });
							  // 如果好友列表有变化，则 SDK 会触发 TencentCloudChat.EVENT.FRIEND_LIST_UPDATED 事件
							}).catch(function(imError) {
							  console.warn('removeFromFriendList error:', imError);
							});
						} else {
							this.$refs.toast.show({
								type: 'default',
								message: res.msg,
								duration: 1500
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.userInfo {
		text-align: center;
		border-bottom: 1rpx solid #e8e8e8;
		padding-bottom: 30rpx;
		margin-bottom: 20rpx;

		image {
			width: 212rpx;
			height: 212rpx;
			border-radius: 50%;
		}
	}
</style>
